package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.EpcGoodsInfo;

import java.util.ArrayList;
import java.util.List;

public class UsefulAdapter extends RecyclerView.Adapter<UsefulAdapter.UserfulHolder> {

    private List<EpcGoodsInfo> epcGoodsInfoList;
    private Context context;

    public UsefulAdapter(Context context) {
        this.context = context;
        epcGoodsInfoList = new ArrayList<>();
    }

    public void setData(EpcGoodsInfo epcGoodsInfo){
//        epcGoodsInfoList.clear();

        boolean isContains = false;
        int position = 0;
        if (null != epcGoodsInfo){
            if (epcGoodsInfoList.size() >0) {
                long goodsId = epcGoodsInfo.getGoods_id();
                for (EpcGoodsInfo info : epcGoodsInfoList) {
                    if (goodsId == info.getGoods_id()) {
                        long count = info.getCount();
                        info.setCount(++count);
                        isContains = true;
                        position = epcGoodsInfoList.indexOf(info);
                        break;
                    }
                }
            }
            if (!isContains) {
                epcGoodsInfo.setCount(1);
                epcGoodsInfoList.add(epcGoodsInfo);
                notifyItemRangeChanged(epcGoodsInfoList.size() - 1, 1);
            }else{
                notifyItemChanged(position);
            }
        }
    }

    public List<EpcGoodsInfo> getData(){
        return null == epcGoodsInfoList||epcGoodsInfoList.size() <0?null:epcGoodsInfoList;
    }

    @Override
    public UserfulHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new UserfulHolder(LayoutInflater.from(context).inflate(R.layout.item_userful, parent,false));
    }

    @Override
    public void onBindViewHolder(UserfulHolder holder, int position) {
        EpcGoodsInfo epcGoodsInfo = epcGoodsInfoList.get(position);
        if (null != epcGoodsInfo){
            holder.tv_name.setText("商品名称："+epcGoodsInfo.getGoods_name());
            holder.tv_origin_price.setText("原价"+epcGoodsInfo.getOrigin_price());
            holder.tv_price.setText("现价"+epcGoodsInfo.getGoods_price());
            holder.tv_id.setText("ID:"+epcGoodsInfo.getGoods_id());
            holder.tv_epc_count.setText("数量:"+epcGoodsInfo.getCount());
        }
    }

    @Override
    public int getItemCount() {
        return epcGoodsInfoList.size() > 0 ? epcGoodsInfoList.size() : 0;
    }

    public void clear() {
        epcGoodsInfoList.clear();
        notifyDataSetChanged();
    }

    public class UserfulHolder extends RecyclerView.ViewHolder {
        TextView tv_name,tv_origin_price,tv_price,tv_epc_count,tv_id;

        public UserfulHolder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_origin_price = (TextView) itemView.findViewById(R.id.tv_origin_price);
            tv_price = (TextView) itemView.findViewById(R.id.tv_price);
            tv_epc_count = (TextView) itemView.findViewById(R.id.tv_epc_count);
            tv_id = (TextView) itemView.findViewById(R.id.tv_id);
        }
    }
}
